# -*- coding: utf-8 -*-
from flask import Blueprint, request, jsonify, make_response, redirect, g
import json

from common.libs.user.UserService import UserService
from common.models.User import User
from application import app, db
from common.libs.UrlManager import UrlManager
from common.libs.Helper import ops_render

#定义蓝图
route_user = Blueprint('user_page', __name__)


@route_user.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "GET":
        return ops_render("user/login.html")
    resp = {'code': 200, 'msg': '登录成功', 'data': {}}
    # 所有请求都在这里面
    req = request.values
    # 获取提交参数 判空处理  三元表达式
    login_name = req['login_name'] if 'login_name' in req else ''
    login_pwd = req['login_pwd'] if 'login_pwd' in req else ''

    if login_name is None or len(login_name) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名~"
        return jsonify(resp)

    if login_pwd is None or len(login_pwd) < 1:
        resp['code'] = -1
        resp['msg'] = '请输入正确的登录密码~'
        return jsonify(resp)
    #数据库login_name进行唯一约束
    user_info = User.query.filter_by(login_name=login_name).first()
    if not user_info:
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名和密码~"
        return jsonify(resp)

    # 判断用户数据库的加密密码是否等于加密后的
    if user_info.login_pwd != UserService.genePwd(login_pwd, user_info.login_salt):
        resp['code'] = -1
        resp['msg'] = "请输入正确的登录用户名和密码 -2"
        return jsonify(resp)

    if user_info.status != 1:
        resp['code'] = -1
        resp['msg'] = "账号已被禁用, 请联系管理员处理"
        return jsonify(resp)

    response = make_response(json.dumps(resp))
    # cookie的第二个参数用户的uid 第一个参数是对信息进行加密
    response.set_cookie(app.config['AUTH_COOKIE_NAME'], "%s#%s" % (UserService.geneAuthCode(user_info), user_info.uid), 60 * 60 * 24)
    # return "%s - %s" %(login_name, login_pwd)
    return response


# 编辑登录人信息
@route_user.route("/edit", methods=["GET", "POST"])
def edit():
    if request.method == "GET":
        #传递状态值
        return ops_render("user/edit.html",{'current':'edit'})

    resp = {'code': 200, 'msg': '操作成功', 'data': {}}
    # 获得所有请求值
    req = request.values

    # 获取昵称
    nickname = req['nickname'] if 'nickname' in req else ''
    # 获取邮箱
    email = req['email'] if 'email' in req else ''

    if nickname is None or len(nickname) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入规范的姓名~"
        return jsonify(resp)

    if email is None or len(email) < 1:
        resp['code'] = -1
        resp['msg'] = "请输入规范的邮箱~"
        return jsonify(resp)

    # 获取当前用户的信息并提交数据库
    user_info = g.current_user
    user_info.nickname = nickname
    user_info.email = email

    db.session.add(user_info)
    db.session.commit()
    return jsonify(resp)


@route_user.route("/reset-pwd", methods=["GET", "POST"])
def resetPwd():
    if request.method == "GET":
        return ops_render("user/reset_pwd.html",{'current':'reset-pwd'})
    resp = {'code': 200, 'msg': '操作成功', 'data': {}}  # 获得所有请求值
    req = request.values
    # 获取原密码
    old_password = req['old_password'] if 'old_password' in req else ''
    # 获取新密码
    new_password = req['new_password'] if 'new_password' in req else ''

    if old_password is None or len(old_password) < 6:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的原密码~"
        return jsonify(resp)

    if new_password is None or len(new_password) < 6:
        resp['code'] = -1
        resp['msg'] = "请输入符合规范的新密码~"
        return jsonify(resp)

    if old_password == new_password:
        resp['code']  = -1
        resp['msg'] = "请重新输入一个吧，新密码和原密码不能相同哦"
        return jsonify(resp)

    user_info = g.current_user
    user_info.login_pwd = UserService.genePwd(new_password,user_info.login_salt)

    db.session.add(user_info)
    db.session.commit()

    #重新设置cookie 否则会在登录拦截器直接退出
    response = make_response(json.dumps(resp))
    # cookie的第二个参数用户的uid 第一个参数是对信息进行加密
    response.set_cookie(app.config['AUTH_COOKIE_NAME'], "%s#%s" % (UserService.geneAuthCode(user_info), user_info.uid), 60 * 60 * 24)
    # return "%s - %s" %(login_name, login_pwd)
    return response



#退出逻辑
@route_user.route("/logout")
def logout():
    response = make_response(redirect(UrlManager.buildUrl("/user/login")))
    response.delete_cookie(app.config['AUTH_COOKIE_NAME'])
    return response
